import { ErrorTracking } from "@/monitor/error/error-monitor"
import type { OptionsType } from "@/monitor/monitor-type"
import { Transport } from "./transport/transport"
import { RecordScreen } from "./record/record-screen"
import { Behavior } from "./behavior/behavior"
import { PerformanceTracking } from "./performance/Performance"
import { WhiteScreen } from "./white-screen/white-screen"
import { nanoid } from "nanoid"

// Object.assign 用于将一个或多个源对象的所有可枚举属性复制到目标对象中，并返回目标对象
export class Monitor {
    options: OptionsType
    errorTracking: InstanceType<typeof ErrorTracking> | null = null // 获取ErrorTracking的实例类型
    transport: InstanceType<typeof Transport> | null = null
    behavior: InstanceType<typeof Behavior> | null = null
    performanceTracking: InstanceType<typeof PerformanceTracking> | null = null
    whiteScreen: InstanceType<typeof WhiteScreen> | null = null
    public recordScreen?: RecordScreen // 新增录屏实例
    // 新增状态管理属性
    public hasError: boolean = false
    public recordScreenId: string | null = null

    constructor(options: OptionsType) {
        this.options = Object.assign(
            {
                recordScreen: false,
                sdkVersion: "1.0"
            },
            options
        )
    }

    init() {
        // 初始化子模块
        this.errorTracking = new ErrorTracking(this)
        this.transport = new Transport(this)
        this.behavior = new Behavior(this)
        this.whiteScreen = new WhiteScreen(this)
        // 录屏（按需开启）
        if (this.options.isRecordScreen) {
            // this.recordScreen?.startRecording()
            this.recordScreen = new RecordScreen(this, this.options.duration)
        }
        // 性能检测（按需开启）
        if (this.options.isPerformance) {
            this.performanceTracking = new PerformanceTracking(this)
        }
    }

    // 新增方法：统一生成并设置 recordScreenId
    public generateRecordScreenId(): string {
        this.recordScreenId = nanoid(36) // 或其他UUID生成方式
        return this.recordScreenId
    }

    // 新增方法：重置错误状态
    public resetErrorStatus(): void {
        this.hasError = false
        this.recordScreenId = null
    }
}
